package com.qc31.monitorlib.ui.video.observer

import java.util.*

class ChooseVideoUtil {

    private val observerHashMap = Collections.synchronizedMap(HashMap<String, ChooseObserver>())

    fun attachObserver(key: String, observer: ChooseObserver) {
        observerHashMap[key] = observer
    }

    fun detachObserver(key: String) {
        observerHashMap.remove(key)
    }

    fun setChoose(key: String, isChoose: Boolean) {

        observerHashMap.forEach { (keyed, observer) ->
            if (key == keyed) observer.changeVideo(isChoose)
            else observer.changeVideo(false)
        }
    }

    /** 码流变化监听 */
    private var indexListener: OnVideoIndexListener? = null
    fun setOnVideoIndexListener(indexListener: OnVideoIndexListener) {
        this.indexListener = indexListener
    }

    fun setPass(index: Int) {
        indexListener?.onChange(index)
    }

    interface OnVideoIndexListener {
        fun onChange(index: Int)
    }

    /** 对讲监听 */
    private var talkListener: OnTalkListener? = null
    fun setOnTalkListener(talkListener: OnTalkListener) {
        this.talkListener = talkListener
    }

    fun setTalk(isTalk: Boolean) {
        talkListener?.onChange(isTalk)
    }

    interface OnTalkListener {
        fun onChange(isTalk: Boolean)
    }

    fun clearAll() {
        observerHashMap.clear()
    }

    companion object {
        val instance: ChooseVideoUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            ChooseVideoUtil()
        }
    }
}